'''
Created on May 7, 2012

@author: peng
'''
from datetime import datetime
from time import mktime
import calendar

def week_date(value=None):
    if not value:
        value = datetime.utcnow()
    week_num = value.weekday()
    
    start_day = int(mktime(value.timetuple())) - week_num * 60 * 60 * 24
    start_day = int(start_day / (60 * 60 * 24)) * 60 * 60 * 24
    end_day = start_day + 6 * 60 * 60 * 24
    return datetime.fromtimestamp(start_day), datetime.fromtimestamp(end_day)


def month_date(value=None):
    if not value:
        value = datetime.utcnow()

    month_days = calendar.monthrange(value.year, value.month)[1]
    start_day = datetime(value.year, value.month, 1)
    end_day = datetime(value.year, value.month, month_days)
    return start_day, end_day

print week_date()

print month_date()

